<?php
namespace App\Http\Controllers\Admin;

use App\Models\Menu;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

use App\Rules\Admin\SaveRoleRule;
use App\Rules\Admin\MenuSaveRule;
use App\Services\Admin\RoleServices;
use App\Services\Admin\MenuService;

class RoleController extends Controller
{
    /**
     * 角色排队数据
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function index(Request $request){
        //角色首页
        $roleService = new RoleServices();

        //搜索
        $searchData = $request->input('search');
        //每页显示数量
        $perPage = $request->input('perPage');

//        echo '<pre>';
//        foreach ($roleService->index($request) as $key=>$val){
//            print_r($val->toArray());
//        }
//        echo '<pre>';
//        exit;

        return view('admin.role.index', [
            'searchData' => $searchData,
            'tablePerPage' => !empty($perPage) ? $perPage : 20,
            'listObject' => $roleService->index($request),
        ]);
    }

    /**
     * 添加或编辑角色
     * @param SaveRoleRule $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveRole(SaveRoleRule $request){
        //编辑，增加角色

        if(!$request->isMethod('post')){
            return $this->responseError('请求方式出错');
        }

        $roleServices = new RoleServices();

        return $roleServices->saveRole($request);
    }

    public function editRole(Request $request){
        //编辑,增加，角色
        //超级管理员除外
        return view('admin.role.editRole');
    }

    public function changeRole(Request $request){
        //改变角色状态
        //超级管理员除外
    }

    public function deleteRole(Request $request){
        //删除角色状态
        //超级管理员除外
    }

    public function actionsRole(Request $request){
        //批量操作
        //超级管理员除外
    }

    /**
     * 编辑规则
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function editRules(Request $request){
        //编辑，增加规则
        //超级管理员除外
        //菜单处理
        $menuService = new MenuService();
        //规则处理
        $roleService = new RoleServices();

//        echo '<pre>';
//        print_r($roleService->getRoleObject($request->route('id'))->toArray());
//        echo '<pre>';
//        echo $request->route('id');
//        echo '<pre>';
//        print_r($menuService->getMenuArrWhereIn());
//        echo '<pre>';
//        exit;


        return view('admin.role.editRules',[
            'menuTreeArr' => $menuService->getAllMenuTreeArr(),
            'editObject' => $roleService->getRoleObject($request->route('id')),
        ]);
    }

    /**
     * 编辑，增加则
     * 超级管理员除外
     * @param Request $request
     */
    public function saveRules(Request $request){
        //规则处理
        $roleService = new RoleServices();


        return $roleService->saveRules($request);
    }

    /**
     * 菜单首页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function menuIndex(Request $request){
        //菜单处理
        $menuService = new MenuService();

//        echo '<pre>';
//        print_r($menuService->getAllMenuTreeArr());
//        echo '<pre>';
//        exit;

        //菜单列表
        return view('admin.role.menuIndex', [
            'menuTree' => $menuService->getAllMenuTreeArr()
        ]);
    }

    /**
     * 菜单编辑，添加
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function menuEdit(Request $request){
        //菜单处理
        $menuServices = new MenuService();
//        echo $request->route('id');

        //菜单编辑，增加页面
        return view('admin.role.menuEdit',[
            'fatherArr' => $menuServices->getFatherMenuData(),
            'editObject' => $menuServices->getMenuObject($request->route('id')),
        ]);
    }

    /**
     * 编辑或新增菜单
     * @param MenuSaveRule $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function menuSave(MenuSaveRule $request){
        //菜单 编辑，增加
        if(!$request->isMethod('post')){
            return $this->responseError('请求方式不正确');
        }

//        return $this->responseError(json_encode($request->post()));

        //菜单处理
        $menuService = new MenuService();

        return $menuService->saveMenu($request);
    }

    /**
     * 菜单删除
     * @param Request $request
     */
    public function menuDelete(Request $request){
        //菜单处理
        $menuService = new MenuService();

        if(!$request->isMethod('post')){
            return $this->responseError('请求方式出错');
        }

        //删除菜单
        return $menuService->deleteMenu($request);
    }
}
